package com.example.sunmiv1.adapter;

import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sunmiv1.R;
import com.example.sunmiv1.activity.OnlineOrderActivity;
import com.example.sunmiv1.base.bean;
import com.example.sunmiv1.entity.OnlineOrderBean;
import com.example.sunmiv1.entity.TurnoverBean;
import com.example.sunmiv1.utils.MathUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.content.ContentValues.TAG;
import static android.content.Context.CLIPBOARD_SERVICE;

public class OnlineOrderAdapter extends BaseAdapter {

    private Context context;
//    private List<OnlineOrderBean.OrderBean> list;
private List<TurnoverBean.OrderBean> list;
    ViewHolder viewHolder;
    private Util util;

//    public OnlineOrderAdapter(Context context, List<OnlineOrderBean.OrderBean> list) {
//        this.context = context;
//        this.list = list;
//    }
     public OnlineOrderAdapter(Context context, List<TurnoverBean.OrderBean> list) {
    this.context = context;
    this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
//        if (convertView == null) {
        convertView = LayoutInflater.from(context).inflate(R.layout.online_order_adapter, null);
        viewHolder = new ViewHolder(convertView);
//            convertView.setTag(viewHolder);
//        } else {
//            viewHolder = (ViewHolder) convertView.getTag();
//        }
//        OnlineOrderBean.OrderBean bean = list.get(position);
        TurnoverBean.OrderBean bean = list.get(position);
        Resources resources = context.getResources();
        viewHolder.onlineOrderAdapterOrderName.setText(bean.getOrder_genre());
        viewHolder.onlineOrderAdapterOrderNum.setText(resources.getString(R.string.onlineOrder_orderNum) + bean.getOrder_no());
        viewHolder.onlineOrderAdapterMoney.setText(resources.getString(R.string.onlineOrder_money) + bean.getAll_money());
        viewHolder.onlineOrderAdapterDiscount.setText(resources.getString(R.string.onlineOrder_discountMoney) + MathUtil.add(Double.parseDouble(bean.getVip_dis_money()),
                Double.parseDouble(bean.getIntegral_dis_money()), Double.parseDouble(bean.getCoupon_money())));
        viewHolder.onlineOrderAdapterTime.setText(resources.getString(R.string.onlineOrder_time) + bean.getAdd_time());
        viewHolder.onlineOrderAdapterPayment.setText(resources.getString(R.string.onlineOrder_payment) + bean.getPayname());
        viewHolder.onlineOrderAdapterStatus.setText(resources.getString(R.string.onlineOrder_status) + bean.getStatus());
        String name = bean.getAddress_name() == null ? "無" : bean.getAddress_name();
        String phone = bean.getAddress_phone() == null ? "無" : bean.getAddress_phone();
        String address = bean.getAddress_address() == null ? "無" : bean.getAddress_address();
        String remark = bean.getRemark() == null ? "無" : bean.getRemark();
        viewHolder.onlineOrderAdapterName.setText(resources.getString(R.string.onlineOrder_name) + name);
        viewHolder.onlineOrderAdapterPhone.setText(resources.getString(R.string.onlineOrder_phone) + phone);
        if (!TextUtils.isEmpty(bean.getExtraction_code())){
            viewHolder.onlineOrderAdapterZfm.setVisibility(View.VISIBLE);
            SpannableString spannableString=new SpannableString("祝福碼："+bean.getExtraction_code());
            spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#ff0000")),4,spannableString.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
            viewHolder.onlineOrderAdapterZfm.setText(spannableString);
            viewHolder.onlineOrderAdapterZfm.setOnLongClickListener(v -> {
                ClipboardManager mClipboardManager = (ClipboardManager) context.getSystemService(CLIPBOARD_SERVICE);
                ClipData clipData = ClipData.newPlainText("", bean.getExtraction_code());
                mClipboardManager.setPrimaryClip(clipData);
                Toast.makeText(context, "複製祝福碼成功", Toast.LENGTH_SHORT).show();
                return true;
            });
        }else {
            viewHolder.onlineOrderAdapterZfm.setVisibility(View.GONE);
        }
        viewHolder.onlineOrderAdapterAddress.setText(resources.getString(R.string.onlineOrder_address) + address);
        viewHolder.onlineOrderAdapterRemark.setText(resources.getString(R.string.onlineOrder_remark) + remark);
        if ("1".equals(bean.getPickup_type())) {//自提
            viewHolder.onlineOrderAdapterDelivery.setText(resources.getString(R.string.onlineOrder_delivery) + context.getString(R.string.onlineOrderAdapterTip1));
        } else if ("3".equals(bean.getPickup_type())) {
            viewHolder.onlineOrderAdapterDelivery.setText(resources.getString(R.string.onlineOrder_delivery) + "快遞配送");
        } else {
            if ("1".equals(bean.getDelivery_type())) {
                viewHolder.onlineOrderAdapterDelivery.setText(resources.getString(R.string.onlineOrder_delivery) + context.getString(R.string.onlineOrderAdapterTip2));
            } else {
                viewHolder.onlineOrderAdapterDelivery.setText(resources.getString(R.string.onlineOrder_delivery) + context.getString(R.string.onlineOrderAdapterTip3));
            }
        }
        if ("1".equals(bean.getPickup_type())){
            viewHolder.onlineOrderAdapterPickUpTime.setVisibility(View.VISIBLE);
            viewHolder.onlineOrderAdapterPickUpTime.setText(resources.getString(R.string.onlineOrder_pickuptime)+bean.getUser_select_time());
        }else if ("2".equals(bean.getPickup_type())){
            viewHolder.onlineOrderAdapterPickUpTime.setVisibility(View.VISIBLE);
            viewHolder.onlineOrderAdapterPickUpTime.setText(resources.getString(R.string.onlineOrder_deliverytime)+bean.getUser_select_time());
        }else {
            viewHolder.onlineOrderAdapterPickUpTime.setVisibility(View.GONE);
        }
        final TextView takeIng = viewHolder.onlineOrderAdapterTake;
//        if ("1".equals(bean.getDelivery_type()) || "2".equals(bean.getDelivery_type()) ||
//                "3".equals(bean.getDelivery_type()) || "4".equals(bean.getDelivery_type())) {
//            Log.e(TAG, "getView: bean.getRefund_status(0)======" + bean.getRefund_status() + "=========" + position);
//            switch (bean.getRefund_status()) {
//                case "0":
//                    takeIng.setVisibility(View.VISIBLE);
//                    Log.e(TAG, "getView: bean.getStatus(1)======" + bean.getStatus() + "=========" + position);
//                    if ("未發貨".equals(bean.getStatus())) {
//                        if ("1".equals(bean.getPickup_type())) {
//                            takeIng.setText(R.string.onlineOrderAdapterTip4);
//                        } else if ("2".equals(bean.getPickup_type())) {
//                            if (!TextUtils.isEmpty(bean.getStatusnum()) && Integer.parseInt(bean.getStatusnum()) <= 1) {
//                                takeIng.setText(R.string.onlineOrderAdapterTip5);
//                            }
//                        } else {
//                            takeIng.setVisibility(View.GONE);
//                        }
//                    } else if ("2".equals(bean.getStatus())) {
//                        if ("1".equals(bean.getPickup_type())) {
//                            takeIng.setText(R.string.onlineOrderAdapterTip6);
//                        } else if ("2".equals(bean.getPickup_type())) {
//                            takeIng.setText(R.string.onlineOrderAdapterTip7);
//                        }
//                    } else if ("3".equals(bean.getStatus())) {
//                        if ("1".equals(bean.getPickup_type())) {
//                            takeIng.setText(R.string.onlineOrderAdapterTip8);
//                        } else if ("2".equals(bean.getPickup_type())) {
//                            takeIng.setText(R.string.onlineOrderAdapterTip9);
//                        }
//                    } else {
//                        takeIng.setText(bean.getStatus());
//                    }
//                    break;
//                case "1":
//                    Log.e(TAG, "getView: bean.getStatus(2)======" + bean.getStatus() + "=========" + position);
//                    takeIng.setVisibility(View.GONE);
//                    takeIng.setText(R.string.onlineOrderAdapterTip10);
//                    break;
//                case "2":
//                    Log.e(TAG, "getView: bean.getStatus(3)======" + bean.getStatus() + "=========" + position);
//                    takeIng.setVisibility(View.VISIBLE);
//                    if ("1".equals(bean.getDelivery_type()) || bean.getDelivery_type() == null) {
//                        takeIng.setText(R.string.onlineOrderAdapterTip11);
//                    } else {
//                        if ("1".equals(bean.getStatus())) {
//                            takeIng.setText(R.string.onlineOrderAdapterTip12);
//                        }
//                    }
//                    break;
//                case "3":
//                    Log.e(TAG, "getView: bean.getStatus(4)======" + bean.getStatus() + "=========" + position);
//                    takeIng.setVisibility(View.GONE);
//                    takeIng.setText(R.string.onlineOrderAdapterTip13);
//                    break;
//                default:
//                    Log.e(TAG, "getView: bean.getStatus(5)======" + bean.getStatus() + "=========" + position);
//                    takeIng.setVisibility(View.VISIBLE);
//                    takeIng.setText(bean.getStatus());
//                    break;
//            }
//        }

            if ("2".equals(list.get(position).getDelivery_type()) ||
                    "3".equals(list.get(position).getDelivery_type()) ||
                    "5".equals(list.get(position).getDelivery_type())) {
                if ("0".equals(list.get(position).getRefund_status())) {
                    switch (list.get(position).getOrder_status()) {
                        case 1:
                            takeIng.setVisibility(View.VISIBLE);
                            takeIng.setText("待接單");
                            break;
                        case 2:
                            takeIng.setVisibility(View.VISIBLE);
                            takeIng.setText("待取貨");
                            break;
                        case 3:
                            takeIng.setVisibility(View.VISIBLE);
                            takeIng.setText("配送中");
                            break;
                        case 4:
                            takeIng.setVisibility(View.VISIBLE);
                            takeIng.setText("已完成");
                            break;
//                        case 5:
//                            takeIng.setVisibility(View.VISIBLE);
//                            takeIng.setText("取消配送");
//                            break;
//                        case 7:
//                            takeIng.setVisibility(View.VISIBLE);
//                            takeIng.setText("取消配送");
//                            break;
                    }
                }
            } else {
                switch (OnlineOrderActivity.flag) {
                    case 0://商城訂單
                        switch (list.get(position).getRefund_status()) {
                            case "0":
                                if ("1".equals(list.get(position).getStatus())) {
                                    if ("1".equals(list.get(position).getPickup_type())) {
                                        takeIng.setVisibility(View.VISIBLE);
                                        takeIng.setText("自提");
                                        //    takeIng.setTextColor(context.getResources().getColor(R.color.green));
                                    } else if("2".equals(list.get(position).getPickup_type())){
                                        if (list.get(position).getOrder_status() == 0) {
                                            takeIng.setVisibility(View.VISIBLE);
                                            takeIng.setText("配送");
                                            //    takeIng.setTextColor(context.getResources().getColor(R.color.green));
                                        }
                                    }
                                } else if ("2".equals(list.get(position).getStatus())) {
                                    if ("1".equals(list.get(position).getPickup_type())) {
                                        takeIng.setVisibility(View.VISIBLE);
                                        takeIng.setText("自提");
                                        //    takeIng.setTextColor(context.getResources().getColor(R.color.green));
                                    } else {
                                        takeIng.setVisibility(View.VISIBLE);
                                        takeIng.setText("正在配送");
                                    }
                                } else if ("3".equals(list.get(position).getStatus())) {
                                    if ("1".equals(list.get(position).getPickup_type())) {
                                        takeIng.setVisibility(View.VISIBLE);
                                        takeIng.setText("已自提");
                                        //    takeIng.setTextColor(context.getResources().getColor(R.color.green));
                                    } else {
                                        takeIng.setVisibility(View.VISIBLE);
                                        takeIng.setText("已配送");
                                        //    takeIng.setTextColor(context.getResources().getColor(R.color.green));
                                    }
                                }
                                break;
                            case "1":
                                if ("2".equals(list.get(position).getOrder_type())) {
//                                    takeIng.setVisibility(View.VISIBLE);
//                                    takeIng.setText("退款");
//                                    takeIng.setTextColor(context.getResources().getColor(R.color.red));
                                }
                                break;
                            case "2":
                                if ("1".equals(list.get(position).getDelivery_type()) || list.get(position).getDelivery_type() == null) {
                                    takeIng.setVisibility(View.VISIBLE);
                                    takeIng.setText("已退款");
                                    takeIng.setTextColor(context.getResources().getColor(R.color.red));
                                } else {
                                    if ("1".equals(list.get(position).getStatus())) {
                                        takeIng.setText("取消配送");
                                    }
                                }
                                break;
                            case "3":
                                takeIng.setVisibility(View.VISIBLE);
                                takeIng.setText("部分退款");
                                takeIng.setTextColor(context.getResources().getColor(R.color.red));
                                break;
                            default:

                                break;
                        }
                        break;
                    case 1://拼團訂單
                        switch (list.get(position).getRefund_status()) {
                            case "0":
                                if ("1".equals(list.get(position).getStatus())) {
                                    if ("1".equals(list.get(position).getPickup_type())) {
                                        takeIng.setVisibility(View.VISIBLE);
                                        takeIng.setText("自提");
                                        //    takeIng.setTextColor(context.getResources().getColor(R.color.green));
                                    } else if("2".equals(list.get(position).getPickup_type())){
                                        if (list.get(position).getOrder_status() == 0) {
                                            takeIng.setVisibility(View.VISIBLE);
                                            takeIng.setText("配送");
                                            //    takeIng.setTextColor(context.getResources().getColor(R.color.green));
                                        }
                                    }
                                } else if ("2".equals(list.get(position).getStatus())) {
                                    if ("1".equals(list.get(position).getPickup_type())) {
                                        takeIng.setVisibility(View.VISIBLE);
                                        takeIng.setText("自提");
                                        //    takeIng.setTextColor(context.getResources().getColor(R.color.green));
                                    } else if("2".equals(list.get(position).getPickup_type())){
                                        takeIng.setVisibility(View.VISIBLE);
                                        takeIng.setText("正在配送");
                                        //    takeIng.setTextColor(context.getResources().getColor(R.color.green));
                                    }
                                } else if ("3".equals(list.get(position).getStatus())) {
                                    if ("1".equals(list.get(position).getPickup_type())) {
                                        takeIng.setVisibility(View.VISIBLE);
                                        takeIng.setText("已自提");
                                        //    takeIng.setTextColor(context.getResources().getColor(R.color.green));
                                    } else if("2".equals(list.get(position).getPickup_type())){
                                        takeIng.setVisibility(View.VISIBLE);
                                        takeIng.setText("已配送");
                                        //    takeIng.setTextColor(context.getResources().getColor(R.color.green));
                                    }
                                }
                                break;
                            case "1":
                                if ("2".equals(list.get(position).getOrder_type())) {
                                    takeIng.setVisibility(View.VISIBLE);
                                    takeIng.setText("退款");
                                    takeIng.setTextColor(context.getResources().getColor(R.color.red));
                                }
                                break;
                            case "2":
                                takeIng.setVisibility(View.VISIBLE);
                                takeIng.setText("已退款");
                                takeIng.setTextColor(context.getResources().getColor(R.color.red));
                                break;
                            case "3":
//                                takeIng.setVisibility(View.VISIBLE);
//                                takeIng.setText("部分退款");
//                                takeIng.setTextColor(context.getResources().getColor(R.color.red));
                                break;
                            default:

                                break;
                        }
                        break;
                    case 2:
                        switch (list.get(position).getRefund_status()) {
                            case "0":
                                if ("1".equals(list.get(position).getStatus())) {
                                    if ("1".equals(list.get(position).getPickup_type())) {
                                        takeIng.setVisibility(View.VISIBLE);
                                        takeIng.setText("自提");
                                        //    takeIng.setTextColor(context.getResources().getColor(R.color.green));
                                    } else if("2".equals(list.get(position).getPickup_type())){
                                        if (list.get(position).getOrder_status() == 0) {
                                            takeIng.setVisibility(View.VISIBLE);
                                            takeIng.setText("配送");
                                            //    takeIng.setTextColor(context.getResources().getColor(R.color.green));
                                        }
                                    }
                                } else if ("2".equals(list.get(position).getStatus())) {
                                    if ("1".equals(list.get(position).getPickup_type())) {
                                        takeIng.setVisibility(View.VISIBLE);
                                        takeIng.setText("自提");
                                        //    takeIng.setTextColor(context.getResources().getColor(R.color.green));
                                    } else {
                                        takeIng.setVisibility(View.VISIBLE);
                                        takeIng.setText("正在配送");
                                        //    takeIng.setTextColor(context.getResources().getColor(R.color.green));
                                    }
                                } else if ("3".equals(list.get(position).getStatus())) {
                                    if ("1".equals(list.get(position).getPickup_type())) {
                                        takeIng.setVisibility(View.VISIBLE);
                                        takeIng.setText("已自提");
                                        //    takeIng.setTextColor(context.getResources().getColor(R.color.green));
                                    } else if("2".equals(list.get(position).getPickup_type())){
                                        takeIng.setVisibility(View.VISIBLE);
                                        takeIng.setText("已配送");
                                        //    takeIng.setTextColor(context.getResources().getColor(R.color.green));
                                    }
                                }
                                break;
                            case "1":
                                if ("2".equals(list.get(position).getOrder_type())) {
//                                    takeIng.setVisibility(View.VISIBLE);
//                                    takeIng.setText("退款");
//                                    takeIng.setTextColor(context.getResources().getColor(R.color.red));
                                }
                                break;
                            case "2":
                                takeIng.setVisibility(View.VISIBLE);
                                takeIng.setText("已退款");
                                takeIng.setTextColor(context.getResources().getColor(R.color.red));
                                break;
                            case "3":
//                                takeIng.setVisibility(View.VISIBLE);
//                                takeIng.setText("部分退款");
//                                takeIng.setTextColor(context.getResources().getColor(R.color.red));
                                break;
                            default:

                                break;
                        }
                        break;
                    case 3://預售訂單
                        switch (list.get(position).getRefund_status()) {
                            case "0":
                                if ("1".equals(list.get(position).getStatus())) {
                                    if ("1".equals(list.get(position).getPickup_type())) {
                                        takeIng.setVisibility(View.VISIBLE);
                                        takeIng.setText("自提");
                                        //    takeIng.setTextColor(context.getResources().getColor(R.color.green));
                                    } else if("2".equals(list.get(position).getPickup_type())){
                                        if (list.get(position).getOrder_status() == 0) {
                                            takeIng.setVisibility(View.VISIBLE);
                                            takeIng.setText("配送");
                                            //    takeIng.setTextColor(context.getResources().getColor(R.color.green));
                                        }
                                    }
                                } else if ("2".equals(list.get(position).getStatus())) {
                                    if ("1".equals(list.get(position).getPickup_type())) {
                                        takeIng.setVisibility(View.VISIBLE);
                                        takeIng.setText("自提");
                                        //    takeIng.setTextColor(context.getResources().getColor(R.color.green));
                                    } else if("2".equals(list.get(position).getPickup_type())){
                                        takeIng.setVisibility(View.VISIBLE);
                                        takeIng.setText("正在配送");
                                    }
                                } else if ("3".equals(list.get(position).getStatus())) {
                                    if ("1".equals(list.get(position).getPickup_type())) {
                                        takeIng.setVisibility(View.VISIBLE);
                                        takeIng.setText("已自提");
                                        //    takeIng.setTextColor(context.getResources().getColor(R.color.green));
                                    } else if("2".equals(list.get(position).getPickup_type())){
                                        takeIng.setVisibility(View.VISIBLE);
                                        takeIng.setText("已配送");
                                        //    takeIng.setTextColor(context.getResources().getColor(R.color.green));
                                    }
                                }
                                break;
                            case "1":
                                if ("2".equals(list.get(position).getOrder_type())) {
//                                    takeIng.setVisibility(View.VISIBLE);
//                                    takeIng.setText("退款");
//                                    takeIng.setTextColor(context.getResources().getColor(R.color.red));
                                }
                                break;
                            case "2":
                                if ("1".equals(list.get(position).getDelivery_type()) || list.get(position).getDelivery_type() == null) {
                                    takeIng.setVisibility(View.VISIBLE);
                                    takeIng.setText("已退款");
                                    takeIng.setTextColor(context.getResources().getColor(R.color.red));
                                } else {
//                                    if ("1".equals(list.get(position).getStatus())) {
//                                        takeIng.setText("取消配送");
//                                    }
                                }
                                break;
                            case "3":
                                takeIng.setVisibility(View.VISIBLE);
                                takeIng.setText("部分退款");
                                takeIng.setTextColor(context.getResources().getColor(R.color.red));
                                break;
                            default:

                                break;
                        }
                        break;
                }
            }
        
        final int index = position;
        final TextView looking = viewHolder.onlineOrderAdapterLook;
        looking.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                util.util(index, 1);
            }
        });
        takeIng.setOnClickListener(v -> {
            if ("配送".equals(takeIng.getText().toString()) ||
                    "自提".equals(takeIng.getText().toString()) ||
                    "退款".equals(takeIng.getText().toString()) ||
                    "部分退款".equals(takeIng.getText().toString())) {
                if ("配送".equals(takeIng.getText().toString()) ||
                        "自提".equals(takeIng.getText().toString())) {
                    util.util(index, 2);
                } else {
//                        util.util(index, 3);
                }
            } else {
                Toast.makeText(context, takeIng.getText().toString(), Toast.LENGTH_SHORT).show();
            }
        });
        return convertView;
    }

    public interface Util {
        void util(int index, int type);//前一個參數：點擊的第幾個，第二個參數：點擊的哪個按鈕;1,查看詳情；2,自提配送
    }

    public void onclick(Util util) {
        this.util = util;
    }


    static class ViewHolder {
        @BindView(R.id.onlineOrder_adapter_orderName)
        TextView onlineOrderAdapterOrderName;
        @BindView(R.id.onlineOrder_adapter_orderNum)
        TextView onlineOrderAdapterOrderNum;
        @BindView(R.id.onlineOrder_adapter_money)
        TextView onlineOrderAdapterMoney;
        @BindView(R.id.onlineOrder_adapter_discount)
        TextView onlineOrderAdapterDiscount;
        @BindView(R.id.onlineOrder_adapter_time)
        TextView onlineOrderAdapterTime;
        @BindView(R.id.onlineOrder_adapter_payment)
        TextView onlineOrderAdapterPayment;
        @BindView(R.id.onlineOrder_adapter_status)
        TextView onlineOrderAdapterStatus;
        @BindView(R.id.onlineOrder_adapter_name)
        TextView onlineOrderAdapterName;
        @BindView(R.id.onlineOrder_adapter_phone)
        TextView onlineOrderAdapterPhone;
        @BindView(R.id.onlineOrder_adapter_zfm)
        TextView onlineOrderAdapterZfm;
        @BindView(R.id.onlineOrder_adapter_address)
        TextView onlineOrderAdapterAddress;
        @BindView(R.id.onlineOrder_adapter_delivery)
        TextView onlineOrderAdapterDelivery;
        @BindView(R.id.onlineOrder_adapter_pickuptime)
        TextView onlineOrderAdapterPickUpTime;
        @BindView(R.id.onlineOrder_adapter_look)
        TextView onlineOrderAdapterLook;
        @BindView(R.id.onlineOrder_adapter_take)
        TextView onlineOrderAdapterTake;
        @BindView(R.id.onlineOrder_adapter_remark)
        TextView onlineOrderAdapterRemark;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
